Skip to content

Conversation

@dwalluck
Copy link
Contributor

@dwalluck dwalluck commented Feb 19, 2025

The PackageURL constructor should not require the use of TreeMap
and can just convert the map to a TreeMap if it isn't already.

@dwalluck dwalluck force-pushed the allow-map-in-constructor branch 3 times, most recently from 0d0777e to 7c10d53 Compare February 19, 2025 22:43
public PackageURL(final String type, final String namespace, final String name, final String version,
final Map<String, String> qualifiers, final String subpath)
throws MalformedPackageURLException {
this(type, namespace, name, version, (qualifiers == null) ? null : ((qualifiers instanceof TreeMap) ? (TreeMap<String, String>) qualifiers : new TreeMap<>(qualifiers)), subpath);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
this(type, namespace, name, version, (qualifiers == null) ? null : ((qualifiers instanceof TreeMap) ? (TreeMap<String, String>) qualifiers : new TreeMap<>(qualifiers)), subpath);
this(type, namespace, name, version, (qualifiers == null) ? null : new TreeMap<>(qualifiers), subpath);

This will prevent the caller from unintentionally modifying the state of PackageURL by modifying the map.

Should this one be the canonical constructor that the one above should call?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My version works to avoid making two copies of the map if the original constructor is making a copy, but it's not. I fixed it in #169 to parse the qualifiers and therefore make a copy.

The behavior of the existing code inconsistent. Right now, it's making a copy if you use the String constructor, but not making a copy if you use this constructor. I could close this and move this code to #169 in which case the way I have it written would make more sense, I think.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in a27618b. But, #169 changes the code to make the qualifier parsing happen on both constructors so that the key/value pairs always get normalized.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably it would make sense to merge this after #169.

The `PackageURL` constructor should not require the use of `TreeMap`
and can just convert the map to a `TreeMap` if it isn't already.
@dwalluck dwalluck force-pushed the allow-map-in-constructor branch from a27618b to 80cfd90 Compare March 10, 2025 14:08
@dwalluck dwalluck force-pushed the allow-map-in-constructor branch from 80cfd90 to cfbffe8 Compare March 11, 2025 00:17
Copy link
Collaborator

@jeremylong jeremylong left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@jeremylong jeremylong merged commit 9bf2d2b into package-url:master Mar 11, 2025
2 checks passed
dwalluck added a commit to dwalluck/packageurl-java that referenced this pull request Mar 11, 2025
@dwalluck dwalluck deleted the allow-map-in-constructor branch April 15, 2025 21:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants